﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Aribaccio;
using Aribaccio.Device;
using BDPAutodetect.Device;
using BDPAutodetect.Simcore;
using CommunityToolkit.Mvvm.ComponentModel;

namespace BDPAutodetect.Simulator
{
    /// <summary>
    /// 
    /// </summary>
    public partial class MainWindowModel : ObservableModel
    {
        /// <summary>
        /// 存储菜单管理实例
        /// </summary>
        protected StorageMenuManager MenuMgr { get; set; }
        /// <summary>
        /// 当前程序版本
        /// </summary>
        public string VersionDisplay => Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? string.Empty;
        /// <summary>
        /// 显示标题内容
        /// </summary>
        public string TitleContent => $"BDPAutodetect模拟器V{VersionDisplay}";
        /// <summary>
        /// Tab菜单 当前选中
        /// </summary>
        [ObservableProperty]
        protected MenuTabItem? menuTabItemSelect;
        /// <summary>
        /// Tab菜单集合
        /// </summary>
        [ObservableProperty]
        protected ObservableCollection<MenuTabItem> menuTabItems;

        /// <summary>
        /// 构造函数
        /// </summary>
        public MainWindowModel(StorageMenuManager argMenuManager)
        {
            this.MenuTabItems = new ObservableCollection<MenuTabItem>();
            this.MenuMgr = argMenuManager;
        }

        public void Initialize()
        {
            if (this.MenuTabItems.Count > 0) return;

            var menuItems = this.MenuMgr.GetMenuItems();
            if (menuItems == null || menuItems.Count <= 0) return;

            foreach (var menuItem in menuItems)
            {
                DeviceType deviceType = menuItem.Type.ToEnum<DeviceType>();
                DeviceModule deviceModule = menuItem.Module.ToEnum<DeviceModule>();
                object? meunContent = null;
                switch (deviceType)
                {
                    case DeviceType.SensorSign:
                        meunContent = new SimulatorSensorView(deviceType, deviceModule);
                        break;
                    case DeviceType.Customer:
                    case DeviceType.Electrical:
                    default:
                        meunContent = new SimulatorView(deviceType, deviceModule);
                        break;
                }
                if (meunContent == null) continue;
                var menuTabItem = new MenuTabItem(menuItem, meunContent);
                this.MenuTabItems.Add(menuTabItem);
            }
            //设置默认TabItem
            if (MenuTabItems.Count > 0)
            {
                this.MenuTabItemSelect = MenuTabItems.First();
            }
        }
    }
}
